package com.serve.oa.controller;


import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.serve.oa.common.lang.Result;
import com.serve.oa.entity.SysMenu;
import com.serve.oa.entity.SysRoleMenu;
import com.serve.oa.entity.SysUser;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author __tyw
 * @since 2022-11-16
 */
@RestController
@RequestMapping("/menu")
public class SysMenuController  extends BaseController {

    @GetMapping("list")
    @RequiresPermissions("sys:menu:list")
    public Result list (){


        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
// 自定义属性名 都要默认值的
        treeNodeConfig.setNameKey("name");
        treeNodeConfig.setIdKey("id");
        treeNodeConfig.setParentIdKey("parentId");
        treeNodeConfig.setChildrenKey("children");


//转换器

        List<SysMenu> menus = menuService.list();
        List<Tree<String>> treeNodes = TreeUtil.build(menus, "0",treeNodeConfig,(treeNode, tree)->{
            tree.setId(treeNode.getId().toString());
            tree.setParentId(treeNode.getParentId().toString());
            tree.setName(treeNode.getName());
            tree.putExtra("path", treeNode.getPath());
            tree.putExtra("perms", treeNode.getPerms());
            tree.putExtra("component", treeNode.getComponent());
        });



        //List<SysMenu> menus = menuService.tree();
        return Result.succ(treeNodes);


    }



    @GetMapping("lists")
    public Result lists (){

        List<SysMenu> menus = menuService.list();
        return Result.succ(menus);

    }


    @GetMapping("/{id}")
    public Result getMenuById (@PathVariable("id") Long id){

        Assert.notNull(id, "找不到用户");
        Map list =  userService.myMenu(id);


        return Result.succ(list);

    }

    @GetMapping("nav")
    public Result nav (){
        List<SysMenu> menus = menuService.tree();
        return Result.succ(menus);

    }



}
